import { Controller } from "../../mvc/core/Controller";
import { ITreasureWareHouseController } from "./interfaces/ITreasureWareHouseController";
import { ITreasureWarehourseModule } from "../../../modules/interfaces/ITreasureWarehourseModule";
import { TreasureWarehourseModule } from "../../../modules/TreasureWarehourseModule";
import { TreasureGetItemPanel } from "../view/treasure/TreasureGetItemPanel";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { DataEvent } from "../events/DataEvent";
import { GameController } from "../mvc/GameController";
import { FairylandTreasureData } from "../view/copy/FairylandTreasureData";
import { ItemData } from "../resource/info/ItemData";
import { EPlayerItemPosType } from "../../../Message/Public/EPlayerItemPosType";
import { DragDropData } from "../view/pack/DragDropData";
import { GameProxy } from "../mvc/GameProxy";
import { MsgManager } from "../manager/MsgManager";
import { Language } from "../../../extend/language/Language";
import { FairylandItemShowPanel } from "../view/copy/FairylandItemShowPanel";
//class TreasureWareHouseController
    
    export  class TreasureWareHouseController extends Controller implements ITreasureWareHouseController
    {
        constructor()
        {
            super();this._curItemObj = {};
            
            return;
        }

        public get treasureWarehourse(): ITreasureWarehourseModule
        {
            if (this._treasureWarehourse == null) 
            {
                this._treasureWarehourse = new TreasureWarehourseModule();
            }
            return this._treasureWarehourse;
        }

        public get treasureGetItem(): TreasureGetItemPanel
        {
            if (this._treasureGetItem == null) 
            {
                this._treasureGetItem = new TreasureGetItemPanel();
            }
            return this._treasureGetItem;
        }

        protected /* override */ initServer(): void
        {
            Dispatcher.addEventListener(EventName.TreasureWarehourseModuleOpen, this.onFairylandWarehourseHandler);
            Dispatcher.addEventListener(EventName.TreasureWareHouseCopyStore, this.onCopyStoreHandler);
            Dispatcher.addEventListener(EventName.TreasureMoveToBag, this.onTreasureMoveAllToBagHandler);
            Dispatcher.addEventListener(EventName.TreasureMoveAllToBag, this.onTreasureMoveAllToBagHandler);
            Dispatcher.addEventListener(EventName.TreasureGetItemWindowShow, this.onGetItemWindowShowHandler);
            NetDispatcher.addCmdListener(ServerCommand.BackPackTreasurePackChange, this.onTreasurePackChangeHandler);
            NetDispatcher.addCmdListener(ServerCommand.BackPackTreasureGetItemsChange, this.onTreasureGetItemsChangeHandler);
            return;
        }

        /* internal  */onFairylandWarehourseHandler(arg1: DataEvent): void
        {
            var loc1=cache.copy.isInFairylandCopy();
            this.treasureWarehourse.isInFairylandCopy = loc1;
            if (this.treasureWarehourse.isHide) 
            {
                this.treasureWarehourse.show();
            }
            else 
            {
                this.treasureWarehourse.hide();
            }
            return;
        }

        /* internal  */onCopyStoreHandler(arg1: DataEvent): void
        {
            if (this._fairylandItemShowPanel == null) 
            {
                this._fairylandItemShowPanel = GameController.copy.fairyLandCopy.fairylandItemShowPanel;
            }
            this.treasureWarehourse.isInFairylandCopy = true;
            FairylandTreasureData.addItemAmount();
            FairylandTreasureData.treasureInfos.length = 0;
            this.treasureWarehourse.show();
            this.treasureWarehourse.updateModule();
            this._fairylandItemShowPanel.update(new Array<any>());
            return;
        }

        /* internal  */onTreasureMoveToBagHandler(arg1: DataEvent): void
        {
            var loc11=0;
            var loc12=null;
            var loc1=arg1.data as ItemData;
            var loc2=loc1.data.uid;
            var loc3=cache.pack.backPackCache;
            var loc4=EPlayerItemPosType._EPlayerItemPosTypeTreasure;
            var loc5=EPlayerItemPosType._EPlayerItemPosTypeBag;
            var loc6=this._treasureWarehourse.itemListPanel.selectedIndex;
            var loc7=loc3.itemLength;
            var loc8=loc3.getAllItems();
            var loc9=false;
            var loc10=0;
            while (loc10 < loc8.length) 
            {
                if (!loc3.getItemDataByIndex(loc10 + 1)) 
                {
                    loc11 = cache.pack.treasurePackCache.getIndexByUid(loc2);
                    loc12 = new DragDropData(loc4, loc5, loc2, loc11, loc10 + 1, loc1, null);
                    GameProxy.packProxy.move(loc12);
                    loc9 = true;
                    return;
                }
                ++loc10;
            }
            if (!loc9) 
            {
                MsgManager.showRollTipsMsg(Language.getString(35013));
            }
            return;
        }

        /* internal  */onTreasurePackChangeHandler(arg1: Object): void
        {
            if (!(this._treasureWarehourse == null) && !this._treasureWarehourse.isHide) 
            {
                this._treasureWarehourse.updateModule();
            }
            return;
        }

        /* internal  */onTreasureGetItemsChangeHandler(arg1: Object): void
        {
            if (arg1) 
            {
                this._curItemObj = arg1;
            }
            if (!(this._treasureWarehourse == null) && !this._treasureWarehourse.isHide) 
            {
                this._treasureWarehourse.updateModule();
            }
            return;
        }

        /* internal  */onGetItemWindowShowHandler(arg1: DataEvent): void
        {
            return;
        }

        /* internal  */onTreasureMoveAllToBagHandler(arg1: DataEvent): void
        {
            var loc1=null;
            var loc2=false;
            var loc3=0;
            if (arg1.data != null) 
            {
                loc1 = arg1.data as Map<any, any> /* flash.utils.Dictionary */;
            }
            if (loc1 != null) 
            {
                loc3 = EPlayerItemPosType._EPlayerItemPosTypeBag;
                GameProxy.packProxy.moveBag(EPlayerItemPosType._EPlayerItemPosTypeTreasure, loc3, loc1);
            }
            else 
            {
                GameProxy.packProxy.moveBag(EPlayerItemPosType._EPlayerItemPosTypeTreasure, EPlayerItemPosType._EPlayerItemPosTypeBag);
            }
            return;
        }

        private /* var */_treasureWarehourse: ITreasureWarehourseModule;

        private /* var */_treasureGetItem: TreasureGetItemPanel;

        private /* var */_fairylandItemShowPanel: FairylandItemShowPanel;

        private /* var */_curItemObj: Object;
    }
